package com.zbkj.common.utils;

import sun.util.locale.LocaleUtils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: liuxiaowei
 * @Date: 2022-4-1 01:06
 * @Description:
 */
public class LocalDateUtil {

    private static final String DATE_PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYYMM = "yyyyMM";
    public static final String YYYY_MM = "yyyy-MM";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String formater() {
        return formater(LocalDateTime.now());
    }

    /**
     * LocalDateTime转换为String
     * @param localDateTime
     * @return
     */
    public static String formater(LocalDateTime localDateTime) {
        return formater(localDateTime, DATE_PATTERN_DEFAULT);
    }

    /**
     * LocalDateTime转换为String
     * @param localDateTime
     * @param pattern
     * @return
     */
    public static String formater(LocalDateTime localDateTime, String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * String转为LocalDateTime
     * @param dateStr
     * @return
     */
    public static LocalDateTime parse(String dateStr) {
        return parse(dateStr, DATE_PATTERN_DEFAULT);
    }

    /**
     * String转为LocalDateTime
     * @param dateStr
     * @param pattern
     * @return
     */
    public static LocalDateTime parse(String dateStr, String pattern) {
        return LocalDateTime.parse(dateStr,DateTimeFormatter.ofPattern(pattern));
    }

    public static LocalDateTime plushDays(LocalDateTime localDateTime, long day) {
        return localDateTime.plusDays(day);
    }

    public static LocalDateTime plushHours(LocalDateTime localDateTime, long hours) {
        return localDateTime.plusHours(hours);
    }

    public static LocalDateTime plushSeconds(LocalDateTime localDateTime, long seconds) {
        return localDateTime.plusSeconds(seconds);
    }

    public static LocalDateTime plushMonths(LocalDateTime localDateTime, long months) {
        return localDateTime.plusMonths(months);
    }

    /**
     * 获取时间戳 秒级
     * @param localDateTime
     * @return
     */
    public static long timestampSec(LocalDateTime localDateTime) {
        return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 获取时间戳 毫级
     * @param localDateTime
     * @return
     */
    public static long timestampMill(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    };

    /**
     * Date 转换为 localDateTime
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }

    /**
     * localDateTime转换为Date
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        return Date.from(localDateTime.atZone(zoneId).toInstant());
    }

    /**
     * 取时间所在月份的第几天的时间
     * @param localDateTime
     * @param day
     * @return
     */
    public static LocalDateTime getMonthDay(LocalDateTime localDateTime, int day) {
        /*// 取时间月的第1天：
        LocalDateTime firstDayOfThisMonth = localDateTime.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println("取本月第1天："+firstDayOfThisMonth);*/
        // 取本月第几天：
        return localDateTime.withDayOfMonth(day);
    }

    /**
     * 取月的最后一天
     * @param localDateTime
     * @return
     */
    public static LocalDateTime getMonthLastDay(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.lastDayOfMonth());
    }

    /**
     * 取月第一周的星期几
     * @param localDateTime
     * @param dayOfWeek
     * @return
     */
    public static LocalDateTime getMothFirstWeekDay(LocalDateTime localDateTime, DayOfWeek dayOfWeek) {
        return localDateTime.with(TemporalAdjusters.firstInMonth(dayOfWeek));
    }

    public static LocalDateTime getDayStart(LocalDateTime localDateTime) {
        return LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(), 0,0,0);
    }

    public static LocalDate strToLocalDate(String dateStr, String pattern) {
        return LocalDate.parse(dateStr,DateTimeFormatter.ofPattern(pattern));
    }

    public static String localDateToStr(LocalDate date, String pattern) {
        return date.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static LocalDate localDateLastMothDay(int year, int month) {
        return LocalDate.of(year, month,1).plusMonths(1).minusDays(1);
    }


    public static List<LocalDate> getDateRange(LocalDate startDate, LocalDate endDate) {
        List<LocalDate> dateList = new ArrayList<>();
        while (!startDate.isAfter(endDate)) {
            dateList.add(startDate);
            startDate = startDate.plusDays(1);
        }
        return dateList;
    }

    public static List<String> getMonthRange(YearMonth start, YearMonth end,String pattern) {
        List<String> months = new ArrayList<>();
        YearMonth current = start;
        while (!current.isAfter(end)) {
            months.add(current.format(DateTimeFormatter.ofPattern(pattern)));
            current = current.plusMonths(1);
        }
        return months;
    }

    public static Date addYearForEndDate(long yearNum) {
        LocalDateTime dateTime = LocalDateTime.now();
        dateTime = dateTime.plusYears(yearNum);
        String dateStr = String.format("%s 23:59:59",LocalDateUtil.formater(dateTime,LocalDateUtil.YYYY_MM_DD));
        return DateUtil.strToDate(dateStr,DATE_PATTERN_DEFAULT);
    }

    public static void main(String[] args) {
        /*LocalDateTime localDateTime = LocalDateTime.now();
        //LocalDateTime time = LocalDateUtil.getMonthDay(localDateTime,1);
       //System.out.print(time.toString());
        LocalDateTime localTime = localDateTime.plusYears(1);
        System.out.println(String.format("%s 23:59:59",LocalDateUtil.formater(localTime,LocalDateUtil.YYYY_MM_DD)));*/
        //System.out.println(LocalDateUtil.formater(LocalDateTime.now(),LocalDateUtil.YYYYMMDD));
        /*String s = "2024-08-03";
        String e = "2024-08-03";

        List<LocalDate> list = LocalDateUtil.getDateRange(LocalDateUtil.strToLocalDate(s,LocalDateUtil.YYYY_MM_DD),LocalDateUtil.strToLocalDate(e,LocalDateUtil.YYYY_MM_DD));
        for (LocalDate d : list) {
            System.out.println(LocalDateUtil.localDateToStr(d,LocalDateUtil.YYYY_MM_DD));
        }*/

        /*YearMonth s = YearMonth.of(2023,12);
        YearMonth e = YearMonth.of(2023,11);
        List<String> moths = LocalDateUtil.getMonthRange(s,e,LocalDateUtil.YYYY_MM);
        for (String m : moths) {
            System.out.println(m);
        }*/
        /*String day = "2024-06";
        String [] days = day.split("-");
       LocalDate l = LocalDateUtil.localDateLastMothDay(Integer.valueOf(days[0]),Integer.valueOf(days[1]));
        System.out.println(LocalDateUtil.localDateToStr(l,LocalDateUtil.YYYY_MM_DD));*/
    }
}
